import io from "socket.io-client";
export default class RTCSocket {
    static instance: any;
    socket: any = null;

    // 是否正在连接
    connecting = false;
    count = 0; // 连接次数
    //事件
    onError: any;
    onConnect: any;
    onChatCall: any;
    onSdpCall: any;
    onChatRes: any;
    onSdpRes: any;
    onSdpClose: any;
    onSdp: any;
    onUserConnect: any;
    onChatClose: any;
    onMessage: any;

    //获取socket实例
    static getInstance = () => {
        RTCSocket.instance = new RTCSocket();
        RTCSocket.instance.connect();
        return RTCSocket.instance;
    };
    // 绑定socket事件
    public bindEvents(events: any) {
        this.onError = events.onError || this.onError;
        this.onConnect = events.onConnect || this.onConnect;
        this.onChatCall = events.onChatCall || this.onChatCall;
        this.onSdpCall = events.onSdpCall || this.onSdpCall;
        this.onChatRes = events.onChatRes || this.onChatRes;
        this.onSdpRes = events.onSdpRes || this.onSdpRes;
        this.onSdpClose = events.onSdpClose || this.onSdpClose;
        this.onSdp = events.onSdp || this.onSdp;
        this.onUserConnect = events.onUserConnect || this.onUserConnect;
        this.onChatClose = events.onChatClose || this.onChatClose;
        this.onMessage = events.onMessage || this.onMessage;
    }
    // 重置所有绑定事件
    public offAllEvents() {
        this.onError;
        this.onConnect = null;
        this.onChatCall = null;
        this.onChatRes = null;
        this.onChatClose = null;
        this.onSdpCall = null;
        this.onSdpRes = null;
        this.onSdpClose = null;
        this.onSdp = null;
        this.onUserConnect = null;
        this.onMessage = null;
    }
    // 连接websocket
    public connect() {
        this.connecting = true;
        // let wsBaseUrl = "114.132.218.36:80"; // 线上
        let wsBaseUrl = "localhost:8888"; // 本地
        console.log("wsBaseUrl", wsBaseUrl);
        // 连接socket
        this.socket = (io as any).connect(wsBaseUrl, { transports: ["websocket"] });
        // 监听socket事件
        this.socket.on("connect_error", () => {
            console.log("连接失败");
            this.count++;
            if (this.count >= 10) {
                this.onError();
                throw Error("fail to connect socket");
            }
        });
        this.socket.on("connect", async () => {
            console.log("connect");
            this.connecting = false;
            if (this.onConnect) this.onConnect();
        });
        this.socket.on("chat_call", (data: any) => {
            console.log("chat_call: ", data);
            if (this.onChatCall) this.onChatCall(data);
        });
        this.socket.on("chat_res", (data: any) => {
            console.log("chat_res");
            if (this.onChatRes) this.onChatRes(data);
        });
        this.socket.on("chat_close", (data: any) => {
            console.log("chat_close: ", data);
            if (this.onChatClose) this.onChatClose(data);
        });
        this.socket.on("message", (data: any) => {
            console.log("message", data);
            if (this.onMessage) this.onMessage(data);
        });
        this.socket.on("sdp_call", (data: any) => {
            console.log("sdp_call: ", data);
            if (this.onSdpCall) this.onSdpCall(data);
        });
        this.socket.on("sdp_res", (data: any) => {
            console.log("sdp_res: ", data);
            if (this.onSdpRes) this.onSdpRes(data);
        });
        this.socket.on("sdp_close", (data: any) => {
            console.log("sdp_close: ", data);
            if (this.onSdpClose) this.onSdpClose(data);
        });
        this.socket.on("sdp", (data: any) => {
            console.log("sdp:", data);
            if (this.onSdp) this.onSdp(data);
        });
        this.socket.on("userConnect", (data: any) => {
            console.log("userConnect", data);
            if (this.onUserConnect) this.onUserConnect(data);
        });
    }
}
